package self.hoy.demo.grainmall.search.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import self.hoy.demo.common.exception.BizCodeEnum;
import self.hoy.demo.common.to.es.SpuEsModel;
import self.hoy.demo.common.utils.R;
import self.hoy.demo.grainmall.search.service.ProductService;

import java.io.IOException;
import java.util.List;

/**
 * @author hyh
 * @date 2020/7/19
 */
@Slf4j
@RestController
@RequestMapping("/search")
public class ElasticSaveController {

    @Autowired
    private ProductService productService;

    /**
     * 上架商品
     * @param models
     * @return
     */
    @PostMapping("/product")
    public R productStausUp(@RequestBody List<SpuEsModel> models) {
        try {
            boolean success = productService.productStatusUp(models);
            return success ? R.ok() : R.error(BizCodeEnum.PRODUCT_UP);
        } catch (IOException e) {
            log.error("推送商品到ElasticSearch异常", e);
            return R.error(BizCodeEnum.PRODUCT_UP);
        }
    }
}
